package com.example.mypotiota_si.Adapter;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.mypotiota_si.Datas.Msg;
import com.example.mypotiota_si.R;

import java.util.ArrayList;

public class MsgAdapter extends RecyclerView.Adapter<MsgAdapter.ViewHolder> {
    ArrayList<Msg> mMsgList;
    String TAG="MsgAdapter";
    Context context;

    public MsgAdapter(ArrayList<Msg> mMsgList, Context context) {
        this.mMsgList = mMsgList;
        this.context = context;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view= LayoutInflater.from(parent.getContext()).inflate(R.layout.msg_item,parent,false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        holder.setData(mMsgList.get(position));
        Log.i(TAG,mMsgList.get(position).getType()+"");

    }

    @Override
    public int getItemCount() {
        if (mMsgList!=null){
            return mMsgList.size();
        }
        return 0;
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        RelativeLayout mLeftLayout,mRightLayout;
        TextView leftMsg,rightMsg;
        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            mLeftLayout=itemView.findViewById(R.id.left_layout);
            mRightLayout=itemView.findViewById(R.id.right_layout);
            leftMsg=itemView.findViewById(R.id.left_mag);
            rightMsg=itemView.findViewById(R.id.right_mag);
        }

        public void setData(Msg msg) {
            //判断
            //如果是收到消息，则显示左边的消息布局，将右边隐藏
            if (msg.getType()==0){
                mLeftLayout.setVisibility(View.VISIBLE);//左边可见
                mRightLayout.setVisibility(View.GONE);//右边不可见不占空间
                leftMsg.setText(msg.getContent());//获得文字
            }
            //如果是发生消息，则显示右边的消息布局，将左边隐藏
            if (msg.getType()==1){
                mRightLayout.setVisibility(View.VISIBLE);//you边可见
                mLeftLayout.setVisibility(View.GONE);//zuo边不可见不占空间
                rightMsg.setText(msg.getContent());//获得文字
            }
        }
    }
}
